﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Model;

namespace ConAppCore.Test
{
    unsafe internal class TestSpan
    {
        public static void Test()
        {
            
            string str;
            //PingReply reply;//102.96.129.196

            //reply = ping.Send(IPAddress.Parse("102.96.129.196"), 10);

            //for (int v1 = 1; v1 < 255; v1++)
            Parallel.For(1, 255, v1 =>
            {
                Ping ping = new Ping();
                PingReply reply;
                for (int v2 = 1; v2 < 255; v2++)
                {
                    for (int v3 = 1; v3 < 255; v3++)
                    {
                        //Console.WriteLine($"{v1}.{v2}.{v3}.X");
                        for (int v4 = 1; v4 < 255; v4++)
                        {
                            str = $"{v1}.{v2}.{v3}.{v4}";
                            try
                            {
                                reply = ping.Send(IPAddress.Parse(str), 10);
                                if (reply != null && reply.Status == IPStatus.Success)
                                {
                                    Console.WriteLine($"{str}   OK");
                                    return;
                                }
                            }
                            catch (PingException)
                            {

                            }
                        }
                    }
                }
            });

        }


        private static void TetVVV()
        {
            var arr = new int[6] { 4, 2, 6, 7, 80, 32 };
            // var spanz = new SpanZ<int>(arr);

            // for (int i = 0; i < spanz.Length; i++)
            // {
            //     Console.WriteLine(spanz[i]);
            // }
            ////  spanz.Order();
            // spanz.OrderDescending();
            // Console.WriteLine("----------------------------------");
            // for (int i = 0; i < arr.Length; i++)
            // {
            //     Console.WriteLine(arr[i]);
            // }

            // spanz.FreePinned();
            // return;


            int* ptr0;
            fixed (int* p = arr)
            {
                ptr0 = p;
            }
            int* ptr1 = (int*)Unsafe.AsPointer<int>(ref arr[0]);

            GCHandle handle = GCHandle.Alloc(arr, GCHandleType.Pinned);
            int* ptr2 = (int*)handle.AddrOfPinnedObject();

            fixed (int* ppp = &arr[0])
            {

            }

            int* ptr3 = (int*)Unsafe.AsPointer<int>(ref arr[0]);

            for (int i = 0; i < arr.Length; i++)
            {
                if (i < 3)
                {
                    arr[i] = i;
                }
                else
                {
                    ptr2[i] = i;
                }
            }

            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }

            handle.Free();


            //var xx = new UIntPtr(100);
            //var xx2 = new UIntPtr((ulong)100);

            //UnmanagedMemoryHelper.UnsafeAdpter = new UnsafeAdpter();
            //int[] arr = new int[10];
            //GCHandle handle = GCHandle.Alloc(arr, GCHandleType.Pinned);
            //using (IDisposablerItem item = new DisposablerItem(ref handle, 0))
            //{
            //    int[] pinnedArr = (int[])handle.Target;
            //    Span<int> span = new Span<int>(pinnedArr);
            //    SpanPara(span);

            //    string text = span.ToString();

            //    foreach (var value in span)
            //    {
            //        Console.WriteLine(value);
            //    }
            //}


        }
    }
}
